import { reactive, markRaw, ref, onMounted } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { BaseTableProps } from '@/components/BaseTable/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { IModelListParams } from '@/apis/pcf/myModel/model/modelManage'

import {
  getModelList,
  getPartsModelList,
  getTagList,
  getRecycleCount,
  getPartsRecycleCount
} from '@/apis/pcf/myModel/modelList'
import {
  type SearchOptionsType,
  ColumnSettingTypeEnum,
  type TableFieldsSettingType
} from '@/components/CPageTable'

import { useRoute } from 'vue-router'

export const useModelList = () => {
  const route = useRoute()
  // 是否整车碳足迹模型
  const isFinishedAutomobileModel = route.name === 'MyModelList'

  // 搜索字段及其他
  const searchForm = reactive<IModelListParams>({
    nameOrCode: undefined,
    enterpriseName: undefined,
    modelType: undefined,
    standard: undefined,
    accountYear: undefined,
    accountStatus: (route.query?.accountStatus ?? undefined) as IModelListParams['accountStatus'],
    tag: undefined
  })

  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '产品编码或名称',
      field: 'nameOrCode',
      type: 'input'
    },
    {
      name: '所属企业',
      field: 'enterpriseName',
      type: 'input'
    },
    {
      name: '模型类型',
      field: 'modelType',
      type: 'select',
      fixedOption: 'ModelTypeOptions'
    },
    {
      name: '核算标准',
      field: 'standard',
      type: 'select',
      // 原逻辑乘用车
      // fixedOption: isFinishedAutomobileModel ? 'StandardOptions' : 'itemStandardOption',
      fixedOption: isFinishedAutomobileModel ? 'commercialCarsStandardOption' : 'itemStandardOption'
    },
    {
      name: '核算年份',
      field: 'accountYear',
      type: 'datePicker',
      dateType: 'year',
      format: 'YYYY'
    },
    {
      name: '核算状态',
      field: 'accountStatus',
      type: 'select',
      fixedOption: 'AccountStatusPCFOptions'
    },
    {
      name: '产品标签',
      field: 'tag',
      type: 'select',
      remoteMethod: (query: string): Promise<LabelValueOptions> => {
        console.log('remoteMethod', query)
        return getTagList(query).then((res) => {
          const ans = res.map((item: string) => {
            return {
              label: item,
              value: item
            }
          })
          return ans
        })
      }
    }
  ])

  // 表格配置
  const tableOptions: BaseTableProps = {
    loadData,
    selection: false
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '模型信息',
      field: 'factorNo',
      minWidth: 160,
      slot: 'modelInfo',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '所属企业',
      field: 'enterpriseName',
      minWidth: 160,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '模型类型/核算标准',
      field: 'factorName',
      width: 180,
      slot: 'type',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '功能单位',
      field: 'functionalUnit',
      width: 200,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '核算年份',
      field: 'accountYear',
      width: 85,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '创建人/时间',
      field: 'creator',
      slot: 'creator',
      width: 160,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '核算状态',
      field: 'category',
      slot: 'statusSlot',
      width: 120,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 160,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  function loadData(parameter: BasicPagingListParams) {
    const params = handleParams()
    const getListApi = isFinishedAutomobileModel ? getModelList : getPartsModelList
    Object.assign(params, parameter)
    return getListApi(params)
  }
  const handleParams = (): IModelListParams => {
    let params: IModelListParams = {
      nameOrCode: searchForm.nameOrCode,
      enterpriseName: searchForm.enterpriseName,
      standard: searchForm.standard,
      accountYear: searchForm.accountYear,
      modelType: searchForm.modelType,
      accountStatus: searchForm.accountStatus,
      tag: searchForm.tag
    }
    params = removeUndefinedParams(params)
    return params
  }

  return {
    // 头部右侧筛选项配置
    searchOptions: isFinishedAutomobileModel
      ? searchOptions
      : searchOptions.filter((item) => item.field !== 'modelType'),
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting,
    isFinishedAutomobileModel
  }
}

export const useRecycle = () => {
  // 显示回收站入口
  const showRecycleBtn = ref(false)
  const recycleCount = ref('')
  const route = useRoute()
  // 是否整车碳足迹模型
  const isFinishedAutomobileModel = route.name === 'MyModelList'

  const loadRecycleCount = async () => {
    try {
      const getCountApi = isFinishedAutomobileModel ? getRecycleCount : getPartsRecycleCount
      const count = await getCountApi()
      if (Number(count) > 999) {
        recycleCount.value = '(999+)'
      } else {
        recycleCount.value = `(${count || 0})`
      }
      showRecycleBtn.value = count > 0
    } catch {
      recycleCount.value = ''
    }
  }
  return {
    recycleCount,
    loadRecycleCount,
    showRecycleBtn
  }
}
